import machine
import ubinascii


# 设备ID
CLIENT_ID = ubinascii.hexlify(machine.unique_id()).decode("utf-8")
# MQTT 主题路径
TOPIC = "device/humanbody/{}".format(CLIENT_ID)
TOPIC = TOPIC.encode()

temp_client = None

pin_14 = machine.Pin(14, machine.Pin.IN)


def pin_14_cb(pin):
    if pin.value() == 1:
        temp_client.publish(TOPIC, str("on"))
    else:
        temp_client.publish(TOPIC, str("off"))


pin_14.irq(trigger=machine.Pin.IRQ_RISING | machine.Pin.IRQ_FALLING, handler=pin_14_cb)


def register_humanbody_Topic(client):
    global temp_client
    temp_client = client
    client.subscribe(TOPIC)
